//
//  VisitorLoginView.swift
//  sina weibo
//
//  Created by 万美琳 on 15/12/15.
//  Copyright © 2015年 Eleven. All rights reserved.
//

import UIKit

class VisitorLoginView: UIView {

        //对外提供设置访客视图信息的方法

    func  setUpInfo(tipText:String,imageName:String?) {
    
    tipLable.text = tipText
        if let name = imageName {
            //不是首页
            circle.image = UIImage(named: name)
            largeIcon.hidden = true
            backView.hidden = true
        }else{
            //就是首页
            //就开始做动画
            startAnimation()
        }
    
    }
    
    
    
    
   override init(frame: CGRect) {
    
    super.init(frame:frame)
    
    setUpUI()
    }

   required init?(coder aDecoder: NSCoder) {
//       fatalError("init(coder:) has not been implemented")
    super.init(coder: aDecoder)
   }
   
    
    private func startAnimation(){
    let ani = CABasicAnimation(keyPath: "transform.rotation")
    ani.duration = 20.0
        ani.toValue = 2 * M_PI
    ani.repeatCount = MAXFLOAT
    //当动画完毕或者页面失去活跃状态动画不移除图层
   ani.removedOnCompletion = false
        circle.layer.addAnimation(ani, forKey: nil)
    }
    
    func setUpUI(){
    
        addSubview(circle)
        addSubview(backView)
        addSubview(largeIcon)
        addSubview(tipLable)
        addSubview(registerBtn)
        addSubview(loginBtn)

        //约束需要添加到父视图
        //在使用手码添加约束 需要屏蔽 frame布局
        for subview in subviews {
        subview.translatesAutoresizingMaskIntoConstraints = false
        }
        //largeIcon
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
         addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //  circle
        addConstraint(NSLayoutConstraint(item: circle, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circle, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        //  tipLable
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .CenterX, relatedBy: .Equal, toItem: circle, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Top, relatedBy: .Equal, toItem: circle, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        //registerBtn
         addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLable, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLable, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //backview的约束, addConstraints
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views:["backView":backView,"regBtn":registerBtn]))
        //设置视图的背景颜色
        //UIColor(white: <#T##CGFloat#>, alpha: <#T##CGFloat#>) 设置灰度颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
    }
    
//    override func layoutSubviews() {
//        
//        largeIcon.frame = CGRectMake(50,80 , 100, 100)
//        tipLable.frame = CGRectMake(50, 180, 250, 40)
//        registerBtn.frame = CGRectMake(50,280 , 80, 20)
//        loginBtn.frame = CGRectMake(150,280 , 80, 20)
//
//    }
    
    //遮挡circle视图的背景
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    private lazy var largeIcon : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    private lazy var circle : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    private lazy var tipLable:UILabel = {
    let l = UILabel()
        l.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        l.numberOfLines = 0
        l.font = UIFont.systemFontOfSize(14)
        l.sizeToFit()
    return l
    }()

    private lazy var registerBtn : UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)

    btn.sizeToFit()
    return btn
    }()
    
    private lazy var loginBtn : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.sizeToFit()
        return btn
    }()
}
